// This software is released into the Public Domain.  See copying.txt for details.
package flca.osm.perst.impl;

import java.util.List;

import org.openstreetmap.osmosis.core.domain.v0_6.Tag;
import org.openstreetmap.osmosis.core.domain.v0_6.Way;
import org.openstreetmap.osmosis.core.domain.v0_6.WayNode;

import flca.osm.perst.model.PWay;
import flca.osm.perst.util.FindCornersHelper;
import flca.osm.perst.util.TransactionHelper;


/**
 * Renders a way as xml.
 *
 * @author Brett Henderson
 */
public class WayWriter extends BaseWriter 
{
	/**
	 * Creates a new instance.
	 * 
	 * @param elementName
	 *            The name of the element to be written.
	 * @param indentLevel
	 *            The indent level of the element.
	 */
	public WayWriter(String elementName, int indentLevel) 
	{
	}
	
	
	/**
	 * Writes the way.
	 * 
	 * @param way
	 *            The way to be processed.
	 */
	public void process(Way way) 
	{
		if (isStreet(way)) {
			PWay pway = new PWay((int) way.getId());

			getRoot().wayPk.put(pway);
			addWayNodes(way, pway);
			for (int refnode : pway.getWayNodes()) {
				FindCornersHelper.INSTANCE.addRefNode(refnode, pway.getId());
			}
			
			TransactionHelper.INSTANCE.incWays();
		}
	}

	private boolean isStreet(Way way) {
		if (way.getTags() != null ) {
			for (Tag tag :  way.getTags()) {
				if (tag.toString().toLowerCase().indexOf("highway")>=0) {
					return true;
				}
			}
		}
		return false;
	}
	
	private void addWayNodes(Way way, PWay pway) 
	{
		List<WayNode> wayNodes;
		wayNodes = way.getWayNodes();
		if (wayNodes != null) {
			for (WayNode wayNode : wayNodes) {
				pway.addWayNode((int) wayNode.getNodeId());
			}
		}
	}
}

//showTags(way);
//addTags(way, pway);
//getRoot().wayPk.put(pway);

//private void updateNode(PNode node, int aWayId)
//{
//	node.addWay(aWayId);
//	node.modify();
//}


//private void addTags(Way way, PWay pway) {
//Collection<Tag> tags;
//tags = way.getTags();
//if (tags != null) {
//	for (Tag tag : tags) {
//		pway.addWayTag(tag.getKey(), tag.getValue());
//	}
//}
//}

//private void showTags(Way way)
//{
//	if (way.getTags() != null) {
//		StringBuffer sb = new StringBuffer();
//		for (Tag tag : way.getTags()) {
//			sb.append(tag + ";");
//		}
//		
//		System.out.println(sb);
//	}
//}


